package com.logistic.server.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistic.server.common.utils.DictUtils;
import com.logistic.server.common.utils.SecurityUtils;
import com.logistic.server.entity.common.PageRequest;
import com.logistic.server.entity.common.PageResponse;
import com.logistic.server.entity.pojo.SysDictData;
import com.logistic.server.mapper.SysDictDataMapper;
import com.logistic.server.service.ISysDictDataService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 字典数据 服务实现类
 * </p>
 *
 * @author swen
 * @since 2021-10-25
 */
@Service
@Transactional
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {

    @Resource
    private DictUtils dictUtils;

    /**
     * 根据字典类型查询字典数据信息
     * @param dictType 字典类型
     * @return list
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        List<SysDictData> dictDatas = dictUtils.getDictCache(dictType);
        if (CollectionUtil.isNotEmpty(dictDatas)) {
            return dictDatas;
        }
        dictDatas = baseMapper.selectDictDataByType(dictType);
        if (CollUtil.isNotEmpty(dictDatas)) {
            dictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }

    /**
     * 分页查询列表
     * @param dictData 字典信息
     * @param pageRequest 分页对象
     * @return list
     */
    @Override
    public PageResponse<SysDictData> selectPageDictDataList(SysDictData dictData, PageRequest pageRequest) {
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<SysDictData>()
                .eq(StrUtil.isNotBlank(dictData.getDictType()), SysDictData::getDictType, dictData.getDictType())
                .like(StrUtil.isNotBlank(dictData.getDictLabel()), SysDictData::getDictLabel, dictData.getDictLabel())
                .eq(StrUtil.isNotBlank(dictData.getStatus()), SysDictData::getStatus, dictData.getStatus())
                .orderByAsc(SysDictData::getDictSort);
        IPage<SysDictData> page = new Page<>(pageRequest.getPageIndex(), pageRequest.getPageSize());
        IPage<SysDictData> result = baseMapper.selectPage(page, queryWrapper);
        return PageResponse.<SysDictData>builder().total(result.getTotal()).list(result.getRecords()).build();
    }

    /**
     * 添加字典
     * @param dictData 字典
     */
    @Override
    public void insertDictData(SysDictData dictData) {
        dictData.setCreateBy(SecurityUtils.getUsername());
        dictData.setCreateTime(new Date());
        baseMapper.insert(dictData);
        List<SysDictData> dictDatas = baseMapper.selectDictDataByType(dictData.getDictType());
        dictUtils.setDictCache(dictData.getDictType(), dictDatas);
    }

    /**
     * 删除字典
     * @param ids ids
     */
    @Override
    public void deleteDictData(Long[] ids) {
        for (Long dictCode : ids) {
            SysDictData data = selectDictDataById(dictCode);
            removeById(dictCode);
            List<SysDictData> dictDatas = baseMapper.selectDictDataByType(data.getDictType());
            dictUtils.setDictCache(data.getDictType(), dictDatas);
        }
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode) {
        return getById(dictCode);
    }

    /**
     * 修改字典
     * @param dictData 字典
     */
    @Override
    public void updateDictData(SysDictData dictData) {
        baseMapper.updateById(dictData);
        List<SysDictData> dictDatas = baseMapper.selectDictDataByType(dictData.getDictType());
        dictUtils.setDictCache(dictData.getDictType(), dictDatas);
    }
}
